import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";

import"@/assets/css/el-reset.css"
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

// import "@/assets/css/base.css"
import qfSubMenu from "qf-sub-menu"

import nprogress from "nprogress"
import 'nprogress/nprogress.css'
Vue.use(qfSubMenu)

Vue.config.productionTip = false;
Vue.use(ElementUI);

//路由守卫
router.beforeEach((to, from, next) => {
  nprogress.start()
  // console.log('to',to)
  // console.log('from',from)
  // console.log('next',next)

  //判断本地存储中是否有token，如果有就放行，没有就让他重新跳转至登录页
  let token = localStorage.getItem('w-token');
  if (token) {
    if (store.state.userMenu.length === 0) {
      //用户的路由信息(菜单信息)没有了,需要重新发起请求
      store.dispatch('FETCH_MENULIST')
        .then(() => {
          //重新激活动态添加的开路由,replace:true 不产生历史记录
          next({ ...to, replace: true })
        })
    } else {
      next()
    }
  } else {
    //2.用户没有登入,不能访问登入页面以外的的页面

    //如果用户访问的路径是login,那么需要放行
    if (to.path === "/login") {
      //刷新页面,刷新页面的目的是为了去掉动态添加的路由
      next()

    } else {
      //如果不是login就拦截,并且跳转到login页
      next({ path: "/login" })

    }

  }

})

router.afterEach((to,next)=>{
  nprogress.done()
  let matched = [...to.matched]
  matched.splice(0,1)
  //调用commit触达mutation更改vuex中存储的breadCrumb
  store.commit("SET_BREADCRUBM",matched)
})

import "./assets/css/common.css"
Vue.use(ElementUI);
new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount("#app");